package kines.ct;

import kines.Person;
import kines.SerializableDB;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.ArrayList;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class Referred extends Person implements SerializableDB{
  /**
   * Prospect Id
   */
  private int prospectId = -1;
  public int getProspectId( ){
    return prospectId;
  }
  public void setProspectId( int prospectId ){
    this.prospectId = prospectId;
  }
  /**
   * Get referred
   * @param prospectId
   * @return List of referred
   * @throws java.lang.Exception
   */
  public static Object[] getData( int prospectId )throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    ArrayList referred = new ArrayList();
    Statement stmt_referred = ConnRs.createStatement( );
    String str_referred = "select * from c_referido where id_prospecto = "+prospectId;
    ResultSet rs_referred = stmt_referred.executeQuery( str_referred );
    while( rs_referred.next() ){
      Referred re = new Referred( );
      re.setFirstName( rs_referred.getString("nombre_referido") );
      String lastNames[]= {rs_referred.getString("apaterno_referido"),rs_referred.getString("amaterno_referido")};
      re.setLastName(lastNames);
      re.setPhone(rs_referred.getString("telefono_referido"));
      re.setProspectId( rs_referred.getInt("id_prospecto") );
      referred.add(re);
    }
    ConnRs.close();
    return referred.toArray();
  }
  public void saveData(HttpSession session) throws Exception {
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_referred = ConnRs.createStatement( );
    String str_referred = "insert into c_referido ( nombre_referido, apaterno_referido, amaterno_referido, telefono_referido, id_prospecto) values ('"+firstName+"','"+lastName[0]+"','"+lastName[1]+"','"+phone+"', "+prospectId+")";
    stmt_referred.executeUpdate( str_referred );
    ConnRs.close();
    session.setAttribute("mensaje", "Referido Agregado Con Exito!!");
  }
  public boolean deleteData() throws Exception {
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method deleteData() not yet implemented.");
  }
}